/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-04-10
 * Time: 20:23
 */
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    public void addIndex(int data, int index) {
        int size = size();
        ListNode node = new ListNode(data);
        if (index == 0) {
            addFirst(data);
        } else if (index == size) {
            addLast(data);
        } else if (index < 0 || index > size) {
            System.out.println("index error");
        }
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }

    public void remove(int data) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == data) {
                if (cur.prev == null) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else if (cur.next == null) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }

    public void removeAll(int data) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == data) {
                if (cur.prev == null) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else if (cur.next == null) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
            }
            cur = cur.next;
        }
    }
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}












